package com.zzyl.redis;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

@SpringBootTest
public class RedisTest {

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @Test
    public void test() {

        System.out.println(redisTemplate);
    }


    @Test
    public void testString(){
        redisTemplate.opsForValue().set("classname","153期");
        String classname = redisTemplate.opsForValue().get("classname");
        System.out.println("classname = " + classname);

        redisTemplate.delete("classname");
    }
    @Test
    public void testHash(){
        redisTemplate.opsForHash().put("studentNo","A100","老弟");
        redisTemplate.opsForHash().put("studentNo","A101","压力");
        redisTemplate.opsForHash().put("studentNo","A102","暴大");
        redisTemplate.opsForHash().put("studentNo","A103","邓帅");
        Object o = redisTemplate.opsForHash().get("studentNo", "A101");
        System.out.println("o = " + o);
        redisTemplate.opsForHash().delete("studentNo","A100");
        Set<Object> studentNo = redisTemplate.opsForHash().keys("studentNo");
        System.out.println("studentNo = " + studentNo);
        List<Object> studentNo1 = redisTemplate.opsForHash().values("studentNo");
        System.out.println("studentNo1 = " + studentNo1);
        redisTemplate.delete("classname");
    }
    @Test
    public void testList(){
        redisTemplate.opsForList().leftPushAll("goods","苹果","橘子","香蕉");

        Long goods = redisTemplate.opsForList().size("goods");
        System.out.println("goods = " + goods);

        List<String> goods1 = redisTemplate.opsForList().range("goods", 0, -1);
        System.out.println("goods1 = " + goods1);

         redisTemplate.opsForList().rightPop("goods");
        List<String> goods2 = redisTemplate.opsForList().range("goods", 0, -1);
        System.out.println("goods2 = " + goods2);

    }
    @Test
    public void  testSet(){
       redisTemplate.opsForSet().add("friend:1","邓帅","桂雪涵","廖永昊","段文立");
       redisTemplate.opsForSet().add("friend:2","邓帅","桂雪涵","曾宪泽","段李冰");

        Set<String> members = redisTemplate.opsForSet().members("friend:1");
        System.out.println("members = " + members);

        Long size = redisTemplate.opsForSet().size("friend:1");
        System.out.println("size = " + size);

        Set<String> intersect = redisTemplate.opsForSet().intersect("friend:1", "friend:2");
        System.out.println("intersect = " + intersect);

        Set<String> union = redisTemplate.opsForSet().union("friend:1", "friend:2");
        System.out.println("union = " + union);
    }
    @Test
    public void  testZet(){
        redisTemplate.opsForZSet().add("myzset","叶雅轩",99);
        redisTemplate.opsForZSet().add("myzset","邓帅",60);
        redisTemplate.opsForZSet().add("myzset","廖永昊",50);
        redisTemplate.opsForZSet().add("myzset","曾宪泽",30);

        Set<String> myzset = redisTemplate.opsForZSet().range("myzset", 0, -1);
        System.out.println("myzset = " + myzset);


        redisTemplate.opsForZSet().incrementScore("myzset","叶雅轩",22);
        redisTemplate.opsForZSet().remove("myzset","邓帅","曾宪泽");

        Set<String> myzset1 = redisTemplate.opsForZSet().range("myzset", 0, -1);
        System.out.println("myzset1 = " + myzset1);
    }





}
